package org.csu.springjpetstore.controller;

import org.csu.springjpetstore.domain.*;
import org.csu.springjpetstore.service.CatalogServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @projectName: SpringJPetstore
 * @package: org.csu.springjpetstore.controller
 * @className: categoryController
 * @author: suqianbei
 * @description: TODO
 * @date: 2022/3/13 14:36
 * @version: 1.0
 */
@Controller
@RequestMapping("/catalog")
@SessionAttributes(value = {"category", "cart", "loginUser"})
public class CatalogController {

    @Autowired
    CatalogServiceImpl catalogServiceImpl;

    @GetMapping("/main")
    public String viewMain(@Autowired HttpServletRequest request, Model model, @Autowired HttpSession session) {
        request.getSession();
        if (session.getAttribute("cart") == null) {
            Cart cart = new Cart();
            Account user = new Account();
            model.addAttribute("cart", cart);
            model.addAttribute("loginUser", user);
        }
        List<Category> categoryList = catalogServiceImpl.getCategoryList();
        //测试
        System.out.println("-----------------------------------");
        System.out.println(categoryList);
        model.addAttribute("categoryList", categoryList);
        return "catalog/Main";
    }

    @GetMapping("/help")
    public String viewHelp() {
        return "catalog/Help";
    }

    @GetMapping("ViewCategory")
    public String ViewCategory(String categoryId, Model model) {
        List<Product> productList = catalogServiceImpl.getProductListByCategory(categoryId);
        Category category = catalogServiceImpl.getCategory(categoryId);
        System.out.println(productList + "," + category);
        model.addAttribute("productList", productList);
        model.addAttribute("category", category);
        return "catalog/Category";
    }

    @GetMapping("/ViewProduct")
    public String ViewProduct(String productId, Model model) {
        List<Item> itemList = catalogServiceImpl.getItemListByProduct(productId);
        Product product = catalogServiceImpl.getProduct(productId);
        for (Item item : itemList) {

            System.out.println("----@@@@" + item.getProduct().getProductId() + "," + item.getListPrice());
        }
        model.addAttribute("itemList", itemList);
        model.addAttribute("product", product);
        return "catalog/Product";
    }

    @GetMapping("/ViewItem")
    public String ViewItem(String itemId, Model model) {
        Item item = catalogServiceImpl.getItem(itemId);
        Product product = catalogServiceImpl.getProduct(item.getProduct().getProductId());
        model.addAttribute("item", item);
        model.addAttribute("product", product);
        return "catalog/Item";
    }

    @ResponseBody
    @GetMapping("/findProduct")
    public List<Product> FindProduct(String keyword, Model model) {
        List<Product> productList = catalogServiceImpl.searchProductList(keyword);
        model.addAttribute("productList", productList);
        return productList;
    }

    @PostMapping("/search")
    public String SearchProduct(String keyword, Model model) {
        List<Product> productList = catalogServiceImpl.searchProductList(keyword);

        model.addAttribute("productList", productList);
        model.addAttribute("keyword", keyword);
        return "catalog/SearchProduct";
    }
}
